﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace YMImportWeb.Common
{
    public class LogHelper
    {
        private static string logPath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\";

        /// <summary>
        /// 写入常规日志
        /// </summary>
        public static void Info(string msg)
        {
            Write(msg, "Info");
        }

        /// <summary>
        /// 写入调试日志
        /// </summary>
        public static void Debug(string msg)
        {
            Write(msg, "Debug");
        }

        /// <summary>
        /// 写入错误日志
        /// </summary>
        public static void Error(string msg)
        {
            Write(msg, "Error");
        }

        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="msg">消息内容</param>
        /// <param name="type">消息类型</param>
        private static void Write(string msg, string type)
        {
            StreamWriter writer = null;
            try
            {
                string path = System.AppDomain.CurrentDomain.BaseDirectory + "Logs\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
                string dir = System.IO.Path.GetDirectoryName(path);
                if (!System.IO.Directory.Exists(dir))
                {
                    System.IO.Directory.CreateDirectory(dir);
                }
                writer = new StreamWriter(path, true);
                writer.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "] [" + type + "]\t" + msg);
                writer.Flush();
                writer.Close();
            }
            catch
            {
                writer.Close();
            }
        }
    }
}